home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / iwf12.zip / MODULES / RENDER.C < prev    next >
C/C++ Source or Header  |  1994-02-20  |  14KB  |  448 lines

  1. /*
  2. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3. PROFIL3.C (PROFILE procedures)
  4.  
  5.  
  6. AUTHOR(s): Craig Muller 1992
  7.  
  8. Decription:
  9.  
  10. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. */
  12. #include <windows.h>
  13.  
  14. #pragma hdrstop
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include "iwf.h"
  19.  
  20. // Defines.
  21. #define  MODULENAME "Render"
  22. #define  CLASSNAME  MODULENAME"Popup"
  23.  
  24. //=======================================
  25. // Exported procedures.
  26. //=======================================
  27. long far PASCAL _export WP_Render(HWND hWnd,WORD wMsg,WORD wParam,LONG lParam);
  28. BOOL FAR PASCAL _export DP_Render(HWND hDlg,WORD wMsg,WORD wParam,LONG lParam);
  29.  
  30. //-------------------------------------
  31. // Private procedures.
  32. //-------------------------------------
  33. void CreateThermoPalEntry(PALETTEENTRY *peThermo);
  34. static void render(HDC hDC,IMAGE *image);
  35.  
  36. // Flags
  37.  
  38. // Private variables.
  39. static int resolutions[]={1,2,5,10};
  40. static int polyres =3;
  41. static int usecolor=TRUE;
  42. static int usegrid =TRUE;
  43.  
  44. static HPALETTE hPalColor;             // Handle to color scale palette
  45.  
  46. static HWND hWndRender=NULL;           // Render user window handle.
  47.  
  48. static HINSTANCE hInst;                  // Application instance handle.
  49.  
  50. /*
  51. ---------------------------------------------------------------------
  52. fb_setpalette()
  53. ~~~~~~~~~~~~~~~~
  54.  
  55. DESCRIPTION:
  56. ---------------------------------------------------------------------
  57. */
  58. void fb_SetPaletteEntries(IMAGE *image,int iStart,int cEntries,PALETTEENTRY *pe)
  59.     {
  60.     int i;
  61.  
  62.     for (i=0; i<cEntries; i++)
  63.         {
  64.         image->LUT[iStart+i].peRed   = pe[i].peRed;
  65.         image->LUT[iStart+i].peGreen = pe[i].peGreen;
  66.         image->LUT[iStart+i].peBlue  = pe[i].peBlue;
  67.         image->LUT[iStart+i].peFlags = pe[i].peFlags;
  68.         }
  69.     }
  70.  
  71. /*
  72. --------------------------------------------------------------------------
  73. Render()
  74. ~~~~~~~~
  75.  
  76. PROFILER STARTUP PROCESSING PROCEDURE.
  77. This is called when the user selects this option from the main menu.
  78. This procedure check to see if the popup window exists. If so it simply
  79. passes focus to the window and returns since there is nothing more to do.
  80. If it does not exist then it proceeds to create the popup window and menu
  81. and displays it on the screen. The popup window acts like a sub-program
  82. within the main program and receives messages from the main program.
  83. --------------------------------------------------------------------------
  84. */
  85. void Render(HWND hWndParent)
  86.    {
  87.    char   msg[80];
  88.    WNDCLASS  wc;
  89.  
  90.    // If an instance exists then set focus to that instance and return.
  91.    if (IsWindow(hWndRender))
  92.       {
  93.       ShowWindow(hWndRender,SW_SHOW);
  94.       SetFocus(hWndRender);
  95.       return;
  96.       }
  97.  
  98.    hInst = GetWindowWord(hWndParent,GWW_HINSTANCE);
  99.  
  100.    // Register a new window class for the popup window.
  101.    if (!GetClassInfo(hInst,CLASSNAME,&wc))
  102.       {
  103.       memset(&wc,0,sizeof(WNDCLASS));            // Zero init structure.
  104.  
  105.       wc.lpszClassName = CLASSNAME;              // Name for CreateWindow.
  106.       wc.style         = CS_SAVEBITS;            // Extra style parameters.
  107.       wc.lpfnWndProc   = (WNDPROC)WP_Render;     // Func to get msgs
  108.       wc.hInstance     = hInst;                  // App that owns the class
  109.       wc.hIcon         = LoadIcon(hInst,MODULENAME);
  110.       wc.lpszMenuName  = MODULENAME;
  111.       wc.hbrBackground = GetStockObject(LTGRAY_BRUSH);
  112.  
  113.       if (!RegisterClass(&wc))                   // Initialize new window class.
  114.          {
  115.          char sz[80];
  116.  
  117.          sprintf(sz,"RegisterClass() failure! -> %s",CLASSNAME);
  118.          MessageBeep(MB_ICONEXCLAMATION);
  119.          MessageBox(NULL,sz,NULL,MB_OK | MB_ICONEXCLAMATION);
  120.          return;                                 // Exit on failure
  121.          }
  122.       }
  123.  
  124.    // Window class is registered, now create an window from the class.
  125.    hWndRender = CreateWindow(CLASSNAME,MODULENAME,
  126.                              WS_POPUPWINDOW | WS_CAPTION | WS_MINIMIZEBOX,
  127.                             200,100,770,400,hWndParent,NULL,hInst,NULL);
  128.    if (hWndRender)
  129.       {
  130.       ShowWindow(hWndRender,SW_SHOW);
  131.       SetFocus(hWndRender);
  132.       }
  133.    else
  134.       {
  135.       sprintf(msg,"CreateWindow() failure! -> %s",CLASSNAME);
  136.       MessageBeep(MB_ICONEXCLAMATION);
  137.         MessageBox(NULL,msg,NULL,MB_OK | MB_ICONEXCLAMATION);
  138.       }
  139.    }
  140.  
  141.  
  142.  
  143. /*
  144. ---------------------------------------------------------------------
  145. long far PASCAL _export WP_Render(HWND hWnd,WORD wMsg,WORD wParam,LONG lParam);
  146.  
  147. Description:
  148.  
  149. Local Window Procedure for processing image analysis. This is
  150. registered in the header module.h.
  151.  
  152. This procedure handles a subset of the Windows message loop for
  153. processing input messages.
  154. ---------------------------------------------------------------------
  155. */
  156. long far PASCAL _export WP_Render(HWND hWnd,WORD wMsg,WORD wParam,LONG lParam)
  157.    {
  158.    IMAGE *image;
  159.  
  160.    if (IsWindow(hWndSrc))
  161.       image = (IMAGE *)GetWindowWord(hWndSrc,2);       // Get attached data set.
  162.    else
  163.       image = NULL;
  164.  
  165.    switch (wMsg)
  166.       {
  167.       case WM_CREATE:
  168.       hWndMod = hWnd;                            // Make this module active.
  169.       break;
  170.  
  171.       case WM_PAINT:
  172.       {
  173.       PAINTSTRUCT ps;
  174.  
  175.       BeginPaint(hWnd,&ps);
  176.       EndPaint(hWnd,&ps);
  177.       }
  178.       break;
  179.  
  180.       case WM_COMMAND:
  181.       if (!image)
  182.          {
  183.          MessageBox(NULL,"No image is selected.","Render Tool",MB_OK);
  184.          break;
  185.          }
  186.  
  187.       switch (wParam)
  188.           {
  189.          case 110:
  190.          {
  191.          RECT rc;
  192.          HDC hDC;
  193.  
  194.          hDC = GetDC(hWnd);
  195.          SelectPalette(hDC,hPalMain,FALSE);       // Select palette into DC
  196.          RealizePalette(hDC);                    // Map to system palette.
  197.  
  198.            GetClientRect(hWnd,&rc);
  199.            FillRect(hDC,&rc,GetStockObject(LTGRAY_BRUSH));
  200.          render(hDC,image);                      // Render the 3D profile.
  201.  
  202.          ReleaseDC(hWnd,hDC);
  203.          }
  204.          break;
  205.  
  206.           case 120:
  207.          CallDialogBox(hWnd,DP_Render,MODULENAME);
  208.          break;
  209.          }
  210.       break;
  211.  
  212.       case WM_SETFOCUS:                          // FOCUS HAS BEEN SET.
  213.       if (FlashWindow(hWnd,TRUE))
  214.          FlashWindow(hWnd,TRUE);                 // Make this module active.
  215.       hWndMod = hWnd;                            // Make this module active.
  216.       BringWindowToTop(hWnd);                    // Bring this module to top.
  217.       InvalidateRect(hWnd,NULL,FALSE);           // Invalidate this image.
  218.       UpdateWindow(hWnd);                        // Update this image.
  219.       break;
  220.  
  221.       case WM_KILLFOCUS:                         // FOCUS HAS BEEN KILLED.
  222.       break;
  223.  
  224.       case WM_DESTROY:                           // DESTROY WINDOW.
  225.       DeleteObject(hPalColor);                   // Delete color scale palette.
  226.       break;
  227.  
  228.       default:                                   // Pass on if unprocessed.
  229.       return(DefWindowProc(hWnd, wMsg, wParam, lParam));
  230.       }
  231.  
  232.    lParam=lParam;
  233.  
  234.    return(NULL);
  235.    }
  236.  
  237.  
  238. void Project(int x,int y,int z,POINT *pt)
  239.    {
  240.    pt->x = 240 + x - y/2;
  241.    pt->y = y/2 - z/2;
  242.    }
  243.  
  244. /*
  245. ------------------------------------------------------------------------------
  246. FUNCTION: void Render(HWND hWnd)
  247.  
  248. PURPOSE :
  249. Transfers the contents to the display device for display.
  250.  
  251. COMMENTS:
  252. ------------------------------------------------------------------------------
  253. */
  254. void render(HDC hDC,IMAGE *image)
  255.    {
  256.    int         size,x,y,xoff,yoff;
  257.    BYTE        r,g,b,z,z0,z1,z2,z3;
  258.    POINT       pt[4];
  259.    HBRUSH      hBrush,hOldBrush;
  260.    HPEN        hPen,hOldPen;
  261.  
  262.    ShowCursor(FALSE);
  263.  
  264.    size=resolutions[polyres];
  265.    xoff = 3;
  266.    yoff = 375-240;
  267.    SelectObject(hDC,GetStockObject(BLACK_PEN));
  268.  
  269.    Project(  0,  0,  0,&pt[0]);  MoveTo(hDC,xoff+pt[0].x,yoff+pt[0].y);
  270.    Project(511,  0,  0,&pt[0]);  LineTo(hDC,xoff+pt[0].x,yoff+pt[0].y);
  271.    Project(511,479,  0,&pt[0]);  LineTo(hDC,xoff+pt[0].x,yoff+pt[0].y);
  272.    Project(  0,479,  0,&pt[0]);  LineTo(hDC,xoff+pt[0].x,yoff+pt[0].y);
  273.    Project(  0,  0,  0,&pt[0]);  LineTo(hDC,xoff+pt[0].x,yoff+pt[0].y);
  274.  
  275.    for (y=image->vres-1-size; y>=0; y-=size)
  276.       for (x=0; x<image->hres-1-size; x+=size)
  277.          {
  278.          GetImagePixel(image,x     ,image->vres-1-y   ,&z0);
  279.          GetImagePixel(image,x+size,image->vres-1-y   ,&z1);
  280.          GetImagePixel(image,x+size,image->vres-1-(y+size),&z2);
  281.             GetImagePixel(image,x     ,image->vres-1-(y+size),&z3);
  282.  
  283.          z = (int)(z0+z1+z2+z3)/4;
  284.          r = image->LUT[z].peRed;
  285.          g = image->LUT[z].peGreen;
  286.          b = image->LUT[z].peBlue;
  287.  
  288.          z0 = (int)(image->LUT[z0].peRed+image->LUT[z0].peGreen+image->LUT[z0].peBlue)/3;
  289.          z1 = (int)(image->LUT[z1].peRed+image->LUT[z1].peGreen+image->LUT[z1].peBlue)/3;
  290.          z2 = (int)(image->LUT[z2].peRed+image->LUT[z2].peGreen+image->LUT[z2].peBlue)/3;
  291.          z3 = (int)(image->LUT[z3].peRed+image->LUT[z3].peGreen+image->LUT[z3].peBlue)/3;
  292.  
  293.          z0/=2; z1/=2; z2/=2; z3/=2;
  294.  
  295.          //hPen   = CreatePen(PS_SOLID,1,PALETTEINDEX(usecolor*64 + z/(4+usegrid*2)));
  296.          //hBrush = CreateSolidBrush(PALETTEINDEX(usecolor*64 + z/4));
  297.          hPen   = CreatePen(PS_SOLID,1,PALETTERGB(r/2,g/2,b/2));
  298.          hBrush = CreateSolidBrush(PALETTERGB(r,g,b));
  299.          hOldPen   = SelectObject(hDC,hPen);
  300.          hOldBrush = SelectObject(hDC,hBrush);
  301.  
  302.          pt[0].x = x+y/2       ; pt[0].y = 380-z0-y/2;
  303.          pt[1].x = x+y/2+size  ; pt[1].y = 380-z1-y/2;
  304.          pt[2].x = x+y/2+2*size; pt[2].y = 380-z2-(y/2+size);
  305.          pt[3].x = x+y/2+size  ; pt[3].y = 380-z3-(y/2+size);
  306.  
  307.          Polygon(hDC,pt,4);
  308.  
  309.          SelectObject(hDC,hOldBrush);
  310.          SelectObject(hDC,hOldPen  );
  311.          DeleteObject(hBrush);
  312.          DeleteObject(hPen  );
  313.          }
  314.  
  315.    SelectObject(hDC,GetStockObject(BLACK_PEN));
  316.  
  317.    Project(  0,  0,255,&pt[0]);  MoveTo(hDC,xoff+pt[0].x,yoff+pt[0].y);
  318.    Project(511,  0,255,&pt[0]);  LineTo(hDC,xoff+pt[0].x,yoff+pt[0].y);
  319.    Project(511,479,255,&pt[0]);  LineTo(hDC,xoff+pt[0].x,yoff+pt[0].y);
  320.    Project(  0,479,255,&pt[0]);  LineTo(hDC,xoff+pt[0].x,yoff+pt[0].y);
  321.    Project(  0,  0,255,&pt[0]);  LineTo(hDC,xoff+pt[0].x,yoff+pt[0].y);
  322.  
  323.    ShowCursor(TRUE);
  324.    }
  325.  
  326.  
  327.  
  328. /*
  329. ========================================
  330. Dialog Box Procedures.
  331. ========================================
  332. */
  333.  
  334. #define IDD_RBPOLY1     101
  335. #define IDD_RBPOLY2     102
  336. #define IDD_RBPOLY5     103
  337. #define IDD_RBPOLY10    104
  338. #define IDD_RBGRAY      106
  339. #define IDD_RBCOLOR     107
  340. #define IDD_CBGRID      109
  341.  
  342.  
  343. /*
  344. ==========================================================================
  345. BOOL FAR PASCAL _export DP_Render(HWND hDlg,unsigned msg,WORD wParam,LONG lParam)
  346.  
  347. PURPOSE:
  348. Dialog Box Procedure for selecting various options in
  349. the 3D viewing procedure.
  350. ==========================================================================
  351. */
  352. BOOL FAR PASCAL _export DP_Render(HWND hDlg,WORD wMsg,WORD wParam,LONG lParam)
  353.    {
  354.    static int PolySize;
  355.    static int PalType;
  356.  
  357.    switch (wMsg)
  358.       {
  359.       case WM_PAINT:
  360.       {
  361.       PAINTSTRUCT ps;
  362.       RECT rc;
  363.       HBRUSH hBrush;
  364.  
  365.       BeginPaint(hDlg,&ps);
  366.  
  367.       GetClientRect(hDlg,&rc);
  368.       hBrush = GetStockObject(LTGRAY_BRUSH);
  369.       FillRect(ps.hdc,&rc,hBrush);
  370.  
  371.       EndPaint(hDlg,&ps);
  372.       }
  373.       break;
  374.  
  375.       case WM_INITDIALOG:                        /* message: initialize    */
  376.       PolySize = IDD_RBPOLY1 + polyres;
  377.       PalType  = IDD_RBGRAY + usecolor;
  378.       CheckRadioButton(hDlg,IDD_RBPOLY1,IDD_RBPOLY10,PolySize);
  379.       CheckRadioButton(hDlg,IDD_RBGRAY,IDD_RBCOLOR,PalType);
  380.       CheckDlgButton(hDlg,IDD_CBGRID,usegrid);
  381.       SetFocus(GetDlgItem(hDlg,PalType));
  382.       return (FALSE);                /* Indicates focus is set to a control */
  383.  
  384.       case WM_COMMAND:
  385.       switch (wParam)
  386.          {
  387.          case IDOK:
  388.          usegrid = IsDlgButtonChecked(hDlg,IDD_CBGRID);
  389.          EndDialog(hDlg, TRUE);
  390.          return (TRUE);
  391.  
  392.          case IDCANCEL:
  393.          usegrid = IsDlgButtonChecked(hDlg,IDD_CBGRID);
  394.          EndDialog(hDlg, NULL);
  395.          return (FALSE);
  396.  
  397.          case IDD_RBPOLY1:
  398.          case IDD_RBPOLY2:
  399.          case IDD_RBPOLY5:
  400.          case IDD_RBPOLY10:
  401.          PolySize = wParam;
  402.          polyres = wParam - IDD_RBPOLY1;
  403.          CheckRadioButton(hDlg,IDD_RBPOLY1,IDD_RBPOLY10,PolySize);
  404.          return (TRUE);
  405.  
  406.          case IDD_RBGRAY:
  407.          case IDD_RBCOLOR:
  408.          PalType = wParam;
  409.          usecolor = wParam - IDD_RBGRAY;
  410.          CheckRadioButton(hDlg,IDD_RBGRAY,IDD_RBCOLOR,PalType);
  411.          return (TRUE);
  412.          }
  413.       break;
  414.       }
  415.  
  416.    lParam=lParam;
  417.  
  418.    return FALSE;
  419.    }
  420.  
  421.  
  422. /*
  423. -------------------------------------------------------------------------------
  424.       Create a Color Scale palette and returns a handle to the
  425.       newly created palette. The color intensity ramps for this
  426.       palette are shown by the graphs below.
  427.                         _____
  428.                        /
  429.                       /
  430.                      /
  431.        Red  ________/
  432.             0  16  32  48  64
  433.                     ____
  434.                    /    \
  435.                   /      \
  436.                  /        \
  437.        Green____/          \
  438.             0  16  32  48  64
  439.                 ____
  440.                /    \
  441.               /      \
  442.              /        \
  443.        Blue /          \_____
  444.             0  16  32  48  64
  445. -------------------------------------------------------------------------------
  446. */
  447.  
  448.